
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 09/24/2011 22:37:22
-- Generated from EDMX file: D:\Projects\myomscl_web\omsData\OmsModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [myomscl_Core];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_ListScreensOrderStatuses]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[OrderStatuses] DROP CONSTRAINT [FK_ListScreensOrderStatuses];
GO
IF OBJECT_ID(N'[dbo].[FK_OrderOrderStatuses]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Orders] DROP CONSTRAINT [FK_OrderOrderStatuses];
GO
IF OBJECT_ID(N'[dbo].[FK_ListScreensDetailsScreen]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[DetailsScreens] DROP CONSTRAINT [FK_ListScreensDetailsScreen];
GO
IF OBJECT_ID(N'[dbo].[FK_OrderValuesObjectTypes]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[OrderValues] DROP CONSTRAINT [FK_OrderValuesObjectTypes];
GO
IF OBJECT_ID(N'[dbo].[FK_OrderValuesOrder]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[OrderValues] DROP CONSTRAINT [FK_OrderValuesOrder];
GO
IF OBJECT_ID(N'[dbo].[FK_DetailsScreenFieldsDetailsScreen]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[DetailsScreenFields] DROP CONSTRAINT [FK_DetailsScreenFieldsDetailsScreen];
GO
IF OBJECT_ID(N'[dbo].[FK_OrganisationUserType]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Organisations] DROP CONSTRAINT [FK_OrganisationUserType];
GO
IF OBJECT_ID(N'[dbo].[FK_UserOrganisation]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Users] DROP CONSTRAINT [FK_UserOrganisation];
GO
IF OBJECT_ID(N'[dbo].[FK_ListColumnsListScreens_ListColumns]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ListColumnsListScreens] DROP CONSTRAINT [FK_ListColumnsListScreens_ListColumns];
GO
IF OBJECT_ID(N'[dbo].[FK_ListColumnsListScreens_ListScreens]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ListColumnsListScreens] DROP CONSTRAINT [FK_ListColumnsListScreens_ListScreens];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Orders]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Orders];
GO
IF OBJECT_ID(N'[dbo].[UserTypes]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UserTypes];
GO
IF OBJECT_ID(N'[dbo].[Users]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Users];
GO
IF OBJECT_ID(N'[dbo].[Organisations]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Organisations];
GO
IF OBJECT_ID(N'[dbo].[ListScreens]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ListScreens];
GO
IF OBJECT_ID(N'[dbo].[OrderStatuses]', 'U') IS NOT NULL
    DROP TABLE [dbo].[OrderStatuses];
GO
IF OBJECT_ID(N'[dbo].[DetailsScreens]', 'U') IS NOT NULL
    DROP TABLE [dbo].[DetailsScreens];
GO
IF OBJECT_ID(N'[dbo].[ObjectTypes]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ObjectTypes];
GO
IF OBJECT_ID(N'[dbo].[OrderValues]', 'U') IS NOT NULL
    DROP TABLE [dbo].[OrderValues];
GO
IF OBJECT_ID(N'[dbo].[DetailsScreenFields]', 'U') IS NOT NULL
    DROP TABLE [dbo].[DetailsScreenFields];
GO
IF OBJECT_ID(N'[dbo].[ListColumns]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ListColumns];
GO
IF OBJECT_ID(N'[dbo].[ListColumnsListScreens]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ListColumnsListScreens];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Orders'
CREATE TABLE [dbo].[Orders] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [createddate] datetime  NOT NULL,
    [OrderStatus_Id] int  NOT NULL
);
GO

-- Creating table 'UserTypes'
CREATE TABLE [dbo].[UserTypes] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Users'
CREATE TABLE [dbo].[Users] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [UserTypeId] int  NOT NULL,
    [FirstName] nvarchar(max)  NOT NULL,
    [LastName] nvarchar(max)  NOT NULL,
    [Login] nvarchar(max)  NOT NULL,
    [Password] nvarchar(max)  NOT NULL,
    [Phone1] nvarchar(max)  NOT NULL,
    [Phone2] nvarchar(max)  NOT NULL,
    [Email] nvarchar(max)  NOT NULL,
    [OrganisationId] int  NOT NULL,
    [Organisation_Id] int  NOT NULL
);
GO

-- Creating table 'Organisations'
CREATE TABLE [dbo].[Organisations] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [City] nvarchar(max)  NOT NULL,
    [Address] nvarchar(max)  NOT NULL,
    [Phone1] nvarchar(max)  NOT NULL,
    [Phone2] nvarchar(max)  NOT NULL,
    [Email] nvarchar(max)  NOT NULL,
    [UserType_Id] int  NOT NULL
);
GO

-- Creating table 'ListScreens'
CREATE TABLE [dbo].[ListScreens] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [Title] nvarchar(max)  NOT NULL,
    [ExportEnabled] bit  NOT NULL,
    [FilterEnabled] bit  NOT NULL,
    [CounterEnabled] bit  NOT NULL,
    [UserTypeId] int  NOT NULL
);
GO

-- Creating table 'OrderStatuses'
CREATE TABLE [dbo].[OrderStatuses] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [ListScreensId] int  NOT NULL,
    [Name] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'DetailsScreens'
CREATE TABLE [dbo].[DetailsScreens] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [ListScreen_Id] int  NOT NULL
);
GO

-- Creating table 'ObjectTypes'
CREATE TABLE [dbo].[ObjectTypes] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [LabelName] nvarchar(max)  NOT NULL,
    [Parameters] nvarchar(max)  NOT NULL,
    [Type] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'OrderValues'
CREATE TABLE [dbo].[OrderValues] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Value] nvarchar(max)  NOT NULL,
    [ObjectType_Id] int  NOT NULL,
    [Order_Id] int  NOT NULL
);
GO

-- Creating table 'DetailsScreenFields'
CREATE TABLE [dbo].[DetailsScreenFields] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [LabelName] nvarchar(max)  NOT NULL,
    [PresentationType] nvarchar(max)  NOT NULL,
    [IsEditable] nvarchar(max)  NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [DetailsScreen_Id] int  NOT NULL
);
GO

-- Creating table 'ListColumns'
CREATE TABLE [dbo].[ListColumns] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Header] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'ListColumnsListScreens'
CREATE TABLE [dbo].[ListColumnsListScreens] (
    [ListColumns_Id] int  NOT NULL,
    [ListScreen_Id] int  NOT NULL
);
GO

-- Creating table 'UserTypeListScreens'
CREATE TABLE [dbo].[UserTypeListScreens] (
    [UserTypes_Id] int  NOT NULL,
    [ListScreens_Id] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'Orders'
ALTER TABLE [dbo].[Orders]
ADD CONSTRAINT [PK_Orders]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'UserTypes'
ALTER TABLE [dbo].[UserTypes]
ADD CONSTRAINT [PK_UserTypes]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [PK_Users]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Organisations'
ALTER TABLE [dbo].[Organisations]
ADD CONSTRAINT [PK_Organisations]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'ListScreens'
ALTER TABLE [dbo].[ListScreens]
ADD CONSTRAINT [PK_ListScreens]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'OrderStatuses'
ALTER TABLE [dbo].[OrderStatuses]
ADD CONSTRAINT [PK_OrderStatuses]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'DetailsScreens'
ALTER TABLE [dbo].[DetailsScreens]
ADD CONSTRAINT [PK_DetailsScreens]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'ObjectTypes'
ALTER TABLE [dbo].[ObjectTypes]
ADD CONSTRAINT [PK_ObjectTypes]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'OrderValues'
ALTER TABLE [dbo].[OrderValues]
ADD CONSTRAINT [PK_OrderValues]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'DetailsScreenFields'
ALTER TABLE [dbo].[DetailsScreenFields]
ADD CONSTRAINT [PK_DetailsScreenFields]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'ListColumns'
ALTER TABLE [dbo].[ListColumns]
ADD CONSTRAINT [PK_ListColumns]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [ListColumns_Id], [ListScreen_Id] in table 'ListColumnsListScreens'
ALTER TABLE [dbo].[ListColumnsListScreens]
ADD CONSTRAINT [PK_ListColumnsListScreens]
    PRIMARY KEY NONCLUSTERED ([ListColumns_Id], [ListScreen_Id] ASC);
GO

-- Creating primary key on [UserTypes_Id], [ListScreens_Id] in table 'UserTypeListScreens'
ALTER TABLE [dbo].[UserTypeListScreens]
ADD CONSTRAINT [PK_UserTypeListScreens]
    PRIMARY KEY NONCLUSTERED ([UserTypes_Id], [ListScreens_Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [ListScreensId] in table 'OrderStatuses'
ALTER TABLE [dbo].[OrderStatuses]
ADD CONSTRAINT [FK_ListScreensOrderStatuses]
    FOREIGN KEY ([ListScreensId])
    REFERENCES [dbo].[ListScreens]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ListScreensOrderStatuses'
CREATE INDEX [IX_FK_ListScreensOrderStatuses]
ON [dbo].[OrderStatuses]
    ([ListScreensId]);
GO

-- Creating foreign key on [OrderStatus_Id] in table 'Orders'
ALTER TABLE [dbo].[Orders]
ADD CONSTRAINT [FK_OrderOrderStatuses]
    FOREIGN KEY ([OrderStatus_Id])
    REFERENCES [dbo].[OrderStatuses]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OrderOrderStatuses'
CREATE INDEX [IX_FK_OrderOrderStatuses]
ON [dbo].[Orders]
    ([OrderStatus_Id]);
GO

-- Creating foreign key on [ListScreen_Id] in table 'DetailsScreens'
ALTER TABLE [dbo].[DetailsScreens]
ADD CONSTRAINT [FK_ListScreensDetailsScreen]
    FOREIGN KEY ([ListScreen_Id])
    REFERENCES [dbo].[ListScreens]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ListScreensDetailsScreen'
CREATE INDEX [IX_FK_ListScreensDetailsScreen]
ON [dbo].[DetailsScreens]
    ([ListScreen_Id]);
GO

-- Creating foreign key on [ObjectType_Id] in table 'OrderValues'
ALTER TABLE [dbo].[OrderValues]
ADD CONSTRAINT [FK_OrderValuesObjectTypes]
    FOREIGN KEY ([ObjectType_Id])
    REFERENCES [dbo].[ObjectTypes]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OrderValuesObjectTypes'
CREATE INDEX [IX_FK_OrderValuesObjectTypes]
ON [dbo].[OrderValues]
    ([ObjectType_Id]);
GO

-- Creating foreign key on [Order_Id] in table 'OrderValues'
ALTER TABLE [dbo].[OrderValues]
ADD CONSTRAINT [FK_OrderValuesOrder]
    FOREIGN KEY ([Order_Id])
    REFERENCES [dbo].[Orders]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OrderValuesOrder'
CREATE INDEX [IX_FK_OrderValuesOrder]
ON [dbo].[OrderValues]
    ([Order_Id]);
GO

-- Creating foreign key on [DetailsScreen_Id] in table 'DetailsScreenFields'
ALTER TABLE [dbo].[DetailsScreenFields]
ADD CONSTRAINT [FK_DetailsScreenFieldsDetailsScreen]
    FOREIGN KEY ([DetailsScreen_Id])
    REFERENCES [dbo].[DetailsScreens]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DetailsScreenFieldsDetailsScreen'
CREATE INDEX [IX_FK_DetailsScreenFieldsDetailsScreen]
ON [dbo].[DetailsScreenFields]
    ([DetailsScreen_Id]);
GO

-- Creating foreign key on [UserType_Id] in table 'Organisations'
ALTER TABLE [dbo].[Organisations]
ADD CONSTRAINT [FK_OrganisationUserType]
    FOREIGN KEY ([UserType_Id])
    REFERENCES [dbo].[UserTypes]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OrganisationUserType'
CREATE INDEX [IX_FK_OrganisationUserType]
ON [dbo].[Organisations]
    ([UserType_Id]);
GO

-- Creating foreign key on [Organisation_Id] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [FK_UserOrganisation]
    FOREIGN KEY ([Organisation_Id])
    REFERENCES [dbo].[Organisations]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserOrganisation'
CREATE INDEX [IX_FK_UserOrganisation]
ON [dbo].[Users]
    ([Organisation_Id]);
GO

-- Creating foreign key on [ListColumns_Id] in table 'ListColumnsListScreens'
ALTER TABLE [dbo].[ListColumnsListScreens]
ADD CONSTRAINT [FK_ListColumnsListScreens_ListColumns]
    FOREIGN KEY ([ListColumns_Id])
    REFERENCES [dbo].[ListColumns]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [ListScreen_Id] in table 'ListColumnsListScreens'
ALTER TABLE [dbo].[ListColumnsListScreens]
ADD CONSTRAINT [FK_ListColumnsListScreens_ListScreens]
    FOREIGN KEY ([ListScreen_Id])
    REFERENCES [dbo].[ListScreens]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ListColumnsListScreens_ListScreens'
CREATE INDEX [IX_FK_ListColumnsListScreens_ListScreens]
ON [dbo].[ListColumnsListScreens]
    ([ListScreen_Id]);
GO

-- Creating foreign key on [UserTypes_Id] in table 'UserTypeListScreens'
ALTER TABLE [dbo].[UserTypeListScreens]
ADD CONSTRAINT [FK_UserTypeListScreens_UserType]
    FOREIGN KEY ([UserTypes_Id])
    REFERENCES [dbo].[UserTypes]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [ListScreens_Id] in table 'UserTypeListScreens'
ALTER TABLE [dbo].[UserTypeListScreens]
ADD CONSTRAINT [FK_UserTypeListScreens_ListScreens]
    FOREIGN KEY ([ListScreens_Id])
    REFERENCES [dbo].[ListScreens]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserTypeListScreens_ListScreens'
CREATE INDEX [IX_FK_UserTypeListScreens_ListScreens]
ON [dbo].[UserTypeListScreens]
    ([ListScreens_Id]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------